package org.qufenqi.api.controller.front;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.qufenqi.api.common.CommonResponse;
import org.qufenqi.api.entity.Cart;
import org.qufenqi.api.entity.Order;
import org.qufenqi.api.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("cart")
public class CartController {

    @Autowired
    private CartService cartService;


    @PostMapping("getcart")
    @ResponseBody
    public CommonResponse<List<Order>> getCart(@RequestBody String userid){
        JSONObject jsonObject = JSON.parseObject(userid);
        String userId = jsonObject.getString("userid");

        return cartService.getCart(userId);
    }

    @PostMapping("getorder")
    @ResponseBody
    public CommonResponse<Order> getOrder(@RequestBody String orderid){
        JSONObject jsonObject = JSON.parseObject(orderid);
        String orderId = jsonObject.getString("orderid");

        return cartService.getOrder(orderId);
    }

    @PostMapping("cancelorder")
    @ResponseBody
    public CommonResponse<String> cancelOrder(@RequestBody String orderid){
        JSONObject jsonObject = JSON.parseObject(orderid);
        String orderId = jsonObject.getString("orderid");

        return cartService.cancelOrder(orderId);
    }

}
